import Vue from 'vue'
import VueRouter from 'vue-router'
import MainRoutes from './main.children';
import NProgress from 'nprogress';

// NProgress.start();  进度开始
// NProgress.done();   进度结束

Vue.use(VueRouter)

const routes = [
    {
        path:'/',
        redirect:'/main'
    },
    {
        path: '/login',
        name: 'login',
        meta:{
            title:'登录'
        },  
        component: () => import(/* webpackChunkName: "login" */ '../views/login/index.vue')
    },
    {
        path: '/main',
        name: 'main',
        meta:{
            title:'主页面'
        }, 
        redirect:'/main/outline', 
        component: () => import(/* webpackChunkName: "main" */ '../views/main/index.vue'),
        children:MainRoutes
    }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to,from,next) => {
    NProgress.start(); 
    let writeArr = ['/login'];
    if(writeArr.findIndex(item => to.fullPath.includes(item)) != -1){
        next();
    }else{
        let token = localStorage.getItem('token') || '';
        if(token){
            next();
        }else{
            next(`/login?prev=${to.fullPath}`)
        }
    }
})

router.afterEach(() => {
    NProgress.done();
    // document.title = to.meta.name;
})

export default router
